﻿// test_thread.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include<thread>
#include<queue>
#include<mutex>
#include<shared_mutex>
#include<chrono>
std::mutex _mtx_queque_source;
std::queue<int> source_queue;
bool isexit{ false };
void productor()
{
    int a{0};
    while (!isexit)
    {
        if (isexit) break;
        
        {
            std::unique_lock<std::mutex> gc(_mtx_queque_source);
            source_queue.push(a++);
        }
        
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}
void comsuer()
{
    while (!isexit)
    {
        if (isexit) break;
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        if (source_queue.size() == 0) continue;

        int a{ -1 };
        {
            std::unique_lock<std::mutex> gc(_mtx_queque_source);
            //a = source_queue.front();
            source_queue.pop();
            a = source_queue.size();
        }
        std::cout << "消费者: " << a << std::endl;

       
    }
}
int main()
{
    ///productor
    auto t_pro = std::thread(productor);
    auto c = std::thread(comsuer);

    std::this_thread::sleep_for(std::chrono::seconds(10));
    isexit = true;
    if (t_pro.joinable()) t_pro.join();
    if (c.joinable()) c.join();
    std::cout << "Hello World!\n";
}

